//
//  MainViewController.swift
//  01_新浪微博
//
//  Created by 黄蓉 on 15/12/12.
//  Copyright © 2015年 黄蓉. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tb  = MainTabBar()
        //偷天换日,偷换tabbar必须在添加子控制器之前
        //利用KVC给tabBar赋值
        setValue(tb, forKeyPath: "tabBar")
        tb.composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
        //添加子控制器
        addChildViewControllers()
    
    }
    
///  点击加号按钮
   @objc private func composeButtonClick() {
       print(__FUNCTION__)
    }

    private func addChildViewControllers() {
        addChildViewControllers(HomeTableViewController(), titleString: "首页", imageName: "tabbar_home")
        addChildViewControllers(MessageTableViewController(), titleString: "消息", imageName: "tabbar_message_center")
        addChildViewControllers(DiscoveryTableViewController(), titleString: "发现", imageName: "tabbar_discover")
        addChildViewControllers(ProfileTableViewController(), titleString: "设置", imageName: "tabbar_profile")
    }
    
    ///  按照参数传进来的控制器,title,图片名称创建设置子控制器的相关属性,并包装一个nav
    private  func addChildViewControllers(vc: UIViewController, titleString:String, imageName:String) {
      
        let nav = UINavigationController(rootViewController: vc)

        ///图片
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")

        ///标题
        vc.title = titleString

        addChildViewController(nav)
    }
}
